<template>
    <div>
        <h3 class="bolder red center" >
            <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
            <router-link to="/business/course" class="pink"> {{course.name}}</router-link>
			&gt;
            <router-link to="/business/chapter" class="pink"> {{chapter.name}}</router-link>
        </h3>
        <p>
            <router-link class="btn btn-white btn-default btn-round" to="/business/chapter">
                <i class="ace-icon fa fa-arrow-left grey"></i>
                返回
            </router-link>
            &nbsp;
            <button class="btn btn-white btn-default btn-round" @click="refresh()">
                <i class="ace-icon fa fa-refresh red"></i>
                刷新
            </button>
            &nbsp;
            <button class="btn btn-white btn-success btn-round" @click="openAddModal()">
                <i class="ace-icon fa fa-edit blue"></i>
                新增
            </button>
        </p>

        <table id="simple-table" class="table  table-bordered table-hover">
            <thead>
            <tr>
                <th class="center">ID</th>
                <th class="center">标题</th>
                <th class="center">VOD</th>
                <th class="center">时长</th>
                <th class="center">收费</th>
                <th class="center">顺序</th>
                <th class="center">操作</th>
            </tr>
            </thead>

            <tbody>
            <tr v-for="section in sections">
                <td class="center" style="width: 150px">{{section.id}}</td>
                <td class="center" style="width: 300px">{{section.title}}</td>
                <td class="center">{{section.vod}}</td>
                <td class="center">{{section.time}}秒</td>
                <td class="center">{{SECTION_CHARGE | optionKV(section.charge)}}</td>
                <td class="center">{{section.sort}}</td>
                <td class="center" >
                    <div class=" btn-group">
                        <button v-on:click="play(section)" class="btn btn-xs btn-info">
                            <i class="ace-icon fa fa-video-camera bigger-120"></i>
                        </button>
						<button v-on:click="openEditModal(section.id)" class="btn btn-white btn-xs btn-info btn-round">
                            <i class="ace-icon fa fa-edit bigger-120"></i>
						</button>
						&nbsp;&nbsp;&nbsp;
						<button v-on:click="deleteById(section.id)" class="btn btn-white btn-xs btn-danger btn-round">
                            <i class="ace-icon fa fa-remove bigger-120"></i>
						</button>
                    </div>

                </td>
            </tr>
            <tr v-if="sections.length === 0">
                <td colspan="7" class="center" style="background-color: lightgrey;font-family: 'Courier New',serif">
                    暂无数据
                </td>
            </tr>
            </tbody>
        </table>
        <!-- 分页组件 -->
        <pagination ref="pagination" v-bind:list="list"></pagination>

        <!-- 模态框 -->
        <div class="modal fade" id="myModal1" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
            <div class="modal-dialog" role="document">
                <div class="modal-content" style="border-color: #aa160e;background-color: lightblue">
                    <div class="modal-header">
                        <button type="button" class="close blue" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title grey bolder"
                            id="myModalLabel">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal" role="form">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">标题</label>
                                <div class="col-sm-10">
                                    <input v-model="section.title" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">课程</label>
								<div class="col-sm-10">
									<input disabled v-model="course.name" class="form-control">
								</div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">大章</label>
                                <div class="col-sm-10">
									<input disabled v-model="chapter.name" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">视频</label>
                                <div class="col-sm-10">
                                    <Vod v-bind:inputId="'video-upload'"
                                         v-bind:text="'上传VOD'"
                                         v-bind:suffixs="['mp4']"
                                         v-bind:use="FILE_USE.COURSE.key"
                                         v-bind:afterUpload="afterUpload">
                                    </Vod>
                                    <div v-show="section.video" class="row">
                                        <div class="col-md-9">
                                            <Player v-bind:playerId="'form-player-div'"
                                                    ref="player"></Player>

                                        </div>
                                        <video v-bind:src="section.video" id="video" controls="controls" class="hidden"></video>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">时长</label>
                                <div class="col-sm-10">
                                    <input v-model="section.time" class="form-control" disabled>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">VOD</label>
                                <div class="col-sm-10">
                                    <input v-model="section.vod" class="form-control" disabled>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">收费</label>
                                <div class="col-sm-10">
                                    <select v-model="section.charge" class="form-control">
                                        <option v-for="o in SECTION_CHARGE" v-bind:value="o.key">{{o.value}}</option>
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">顺序</label>
                                <div class="col-sm-10">
                                    <input oninput = "value=value.replace(/[^\d]/g,'')" v-model="section.sort" class="form-control">
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default btn-round"
                                style="font-family: 'Courier New', Courier, monospace" data-dismiss="modal">
                            关闭
                        </button>
                        <button @click="save()" type="button" class="btn btn-primary btn-round"
                                style="font-family: 'Courier New', Courier, monospace">
                            保存
                        </button>
                    </div>
                </div>
            </div>
        </div>
        <ModalPlayer ref="modalPlayer"></ModalPlayer>
    </div>

</template>

<script>
    import Pagination from "../../components/pagination.vue"
    import Vod from "../../components/vod.vue"
    import Player from "../../components/player.vue"
    import ModalPlayer from "../../components/modal-player.vue"

    export default {
        name: "business-section",
        components: {
            Pagination,
            Vod,
            Player,
            ModalPlayer
        },
        data: function () {
            return {
                sections: [],
                section: {
                    name: "",
                    courseId: ''
                },
                modalTitleArr: {
                    insert: "新增小节",
                    update: "修改小节"
                },
                modalTitle: "",
                SECTION_CHARGE: SECTION_CHARGE,
                chapter: {},
				course: {},
                FILE_USE:FILE_USE
            }
        },
        created() {

        },
        mounted: function () {
            // this.$parent.activeSidebar("business-section-sidebar")
            let _this = this;
            let chapter = SessionStorage.get(SESSION_KEY_CHAPTER) || {};
            let course = SessionStorage.get(SESSION_KEY_COURSE) || {};
            if (Tool.isEmpty(chapter) || Tool.isEmpty(course)) {
                _this.$router.push("/welcome");
            }
            _this.chapter = chapter;
            _this.course = course;
            _this.list();
            //激活课程的样式
            _this.$parent.activeSidebar("business-course-sidebar");
        },
        methods: {
            /*播放视频*/
            play(section){
                let _this = this;
                _this.$refs.modalPlayer.playVod(section.vod);
            },
            /*获取文件的时长*/
            getTime(){
                let _this = this;
                setTimeout(function () {
                    let ele = document.getElementById("video");
                    _this.section.time = parseInt(ele.duration, 10);
                }, 1000);
            },
            /*上传文件之后*/
            afterUpload(data) {
                let _this = this;
                _this.section.video =  data.data.path;
                _this.section.vod = data.data.vod;
                _this.$refs.player.playUrl(data.data.path);
                _this.getTime();
            },
            /* 打开新增模态框 */
            openAddModal() {
                let _this = this;
                _this.clearSection();
                _this.modalTitle = _this.modalTitleArr.insert;
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");

            },
            /*打开编辑模态框*/
            openEditModal(id) {
                let _this = this;
                _this.clearSection();
                _this.modalTitle = _this.modalTitleArr.update;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/business/admin/section/findById/' + id).then((response) => {
                    _this.section = response.data.data;
                });
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");
            },
            refresh() {
                let _this = this;
                Loading.show();
                _this.list(_this.$refs.pagination.page);
                Loading.hide();
            },
            /* 查找小节 */
            list(page = 1) {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER + `/business/admin/section/list/${_this.course.id}/${_this.chapter.id}`, {
                    page: page,
                    size: _this.$refs.pagination.size
                }).then((response) => {
                    _this.sections = response.data.data.list;
                    _this.$refs.pagination.render(page, response.data.data.total);
                })
            },
            /*保存小节信息到后台*/
            save() {
                let _this = this;
                _this.section.video = "";
                if (!_this.section.id) {
                    /*不存在id为新增*/
                    _this.insert();
                } else {
                    /*存在id为修改*/
                    _this.update();
                }
            },
            /*新增小节*/
            insert() {
                let _this = this;
                /*新增校验*/
                /*if (!Validator.require(_this.section.title,"标题")
                || !Validator.length(_this.section.title,"标题",1,50)
                || !Validator.length(_this.section.video,"视频",1,200)) {
                    return;
                }*/
                _this.section.courseId = _this.course.id;
                _this.section.chapterId = _this.chapter.id;


                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/section/save', _this.section).then((response) => {
                    console.log(response);
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                });
            },
            /*修改小节*/
            update() {
                let _this = this;
                /*更新校验*/
                /*if (!Validator.require(_this.section.name,"名称")
                        && !Validator.require(_this.section.courseId, "课程")
                        || !Validator.length(_this.section.courseId,"课程ID",1,19)) {
                    return;
                }*/
                _this.$ajax.put(process.env.VUE_APP_SERVER + '/business/admin/section/update', _this.section).then((response) => {
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                })
            },
            /*删除小节*/
            deleteById(id) {
                let _this = this;
                /*删除提示框*/
                Confirm.show("", () => {
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/section/deleteById/' + id).then((response) => {
                        if (response.data.success) {
                            _this.list(_this.$refs.pagination.page);
                            Toast.success(response.data.message);
                        } else {
                            Toast.error(response.data.message);
                        }
                        $("#myModal1").modal("hide");
                    })
                });
            },
            /*清空模态框的数据*/
            clearSection() {
                let _this = this;
                _this.section = {name: "", courseId: "", id: ""};
                _this.modalTitle = "";
            }
        }
    }
</script>
<style scoped>
    video {
        width: 100%;
        height: auto;
        margin-top: 10px;
    }
</style>
